//
//  ViewController.m
//  thread
//
//  Created by 关旭 on 2019/3/8.
//  Copyright © 2019 guanxu. All rights reserved.
//

#import "ViewController.h"
#import "SimpleNSThreadVC.h"
#import "LockNSThreadVC.h"

#import "SimpleGCD_VC.h"
#import "GCD_Communication_VC.h"
#import "GCD_barrier_VC.h"
#import "GCD_apply_VC.h"
#import "GCD_dispatch_group_VC.h"

#import "Simple_Operation_VC.h"
#import "NSOperationQueue_VC.h"
#import "Dependency_VC.h"
#import "Communication_VC.h"
#import "SafetyOperation_VC.h"
#define screenW self.view.frame.size.width
#define screenH self.view.frame.size.height
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource> {
    NSArray *navArray;
}
@property (nonatomic,strong)UITableView *tableView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"多线程";
    NSArray *threadArr = @[@"simple NSThread create",@"NSThread 锁机制"];
    NSArray *gcdArr = @[@"simple GCD use",@"GCD 线程间的通信",@"GCD 栅栏方法",@"GCD apply快速迭代方法",@"GCD dispatch_group"];
    NSArray *operationArr = @[@"Simple Operation",@"NSOperationQueue",@"Dependency 操作依赖",@"线程间通信",@"线程安全"];
    navArray = @[threadArr,gcdArr,operationArr];
    
    
    [self tableView];
    
}
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return @"NSThread";
    }else if (section == 1) {
        return @"GCD";
    }else{
        return @"NSOperation";
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0://NSThread
            {
                
                if (indexPath.row == 0) {
                    SimpleNSThreadVC *vc = [[SimpleNSThreadVC alloc]init];
                    vc.title = navArray[indexPath.section][indexPath.row];
                    [self.navigationController pushViewController:vc animated:YES];
                }else{
                    LockNSThreadVC *vc = [[LockNSThreadVC alloc]init];
                    vc.title = navArray[indexPath.section][indexPath.row];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                
            }
            break;
        case 1://GCD
            {
                if (indexPath.row == 0) {
                    SimpleGCD_VC *vc = [[SimpleGCD_VC alloc]init];
                    vc.title = navArray[indexPath.section][indexPath.row];
                    [self.navigationController pushViewController:vc animated:YES];
                }else if(indexPath.row == 1){
                    GCD_Communication_VC *vc = [[GCD_Communication_VC alloc]init];
                    vc.title = navArray[indexPath.section][indexPath.row];
                    [self.navigationController pushViewController:vc animated:YES];
                }else if(indexPath.row == 2){
                    GCD_barrier_VC *vc = [[GCD_barrier_VC alloc]init];
                    vc.title = navArray[indexPath.section][indexPath.row];
                    [self.navigationController pushViewController:vc animated:YES];
                }else if(indexPath.row == 3){
                    GCD_apply_VC *vc = [[GCD_apply_VC alloc]init];
                    vc.title = navArray[indexPath.section][indexPath.row];
                    [self.navigationController pushViewController:vc animated:YES];
                }else if(indexPath.row == 4){
                    GCD_dispatch_group_VC *vc = [[GCD_dispatch_group_VC alloc]init];
                    vc.title = navArray[indexPath.section][indexPath.row];
                    [self.navigationController pushViewController:vc animated:YES];
                }
            }
            break;
        case 2:
            {
                if (indexPath.row == 0) {
                    Simple_Operation_VC *vc = [[Simple_Operation_VC alloc]init];
                    vc.title = navArray[indexPath.section][indexPath.row];
                    [self.navigationController pushViewController:vc animated:YES];
                }else if (indexPath.row == 1) {
                    NSOperationQueue_VC *vc = [[NSOperationQueue_VC alloc]init];
                    vc.title = navArray[indexPath.section][indexPath.row];
                    [self.navigationController pushViewController:vc animated:YES];
                }else if (indexPath.row == 2) {
                    Dependency_VC *vc = [[Dependency_VC alloc]init];
                    vc.title = navArray[indexPath.section][indexPath.row];
                    [self.navigationController pushViewController:vc animated:YES];
                }else if (indexPath.row == 3) {
                    Communication_VC *vc = [[Communication_VC alloc]init];
                    vc.title = navArray[indexPath.section][indexPath.row];
                    [self.navigationController pushViewController:vc animated:YES];
                }else if (indexPath.row == 4) {
                    SafetyOperation_VC *vc = [[SafetyOperation_VC alloc]init];
                    vc.title = navArray[indexPath.section][indexPath.row];
                    [self.navigationController pushViewController:vc animated:YES];
                }
            }
            break;
        default:
            break;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return navArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *array = [navArray[section] copy];
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.textLabel.text = navArray[indexPath.section][indexPath.row];
    return cell;
    
    
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, screenW, screenH-84) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
    }
    
    return _tableView;
}


@end
